Ad = require('../models/ad').Ad
dataPage = require('../services/dataPage').queryPage
resErr = require('./util').resErr
util = require("util")

exports.ad_add = (req,res) ->
  ad = new Ad
  ad.add req,(err)->
    if err
      console.log(err)
      resErr(res,err)
    else
      res.json({'success':'true'})

exports.ad_list = (req,res) ->
  params = {}
  #相当于sql里面like的写法
  params.name = {'$regex': req.param("name")}  unless !req.param("name")
  params.code = {'$regex': req.param("code")}  unless !req.param("code")
  params.href = {'$regex': req.param("href")}  unless !req.param("href")
  params.ad_category = req.param('ad_category') unless !req.param('ad_category')

  sorts = {}  
  sort = req.param('sort')
  dir = req.param('dir')
  if dir && sort
    dir = if dir.toUpperCase() == 'ASC' then 1 else -1
    sorts[sort] = dir
  else
    sorts = {sorder:1}   
  
  dataPage Ad,params,sorts,req.param("pageSize") or req.param("limit"),req.param("pageNum") or req.param("page"), (err,results) ->
    if results && results.data
      for dt in results.data
        dt.href = dt.href2 || dt.href
    if err
      resErr(res,err)
    else
      res.json(results)

#有效期内的广告
exports.ad_list_validdate = (req,res) ->
  params = {}
  #相当于sql里面like的写法
  params.name = {'$regex': req.param("name")}  unless !req.param("name")
  params.code = {'$regex': req.param("code")}  unless !req.param("code")
  params.href = {'$regex': req.param("href")}  unless !req.param("href")
  params.ad_category = req.param('ad_category') unless !req.param('ad_category')
  today = new Date().toFormat('YYYYMMDD')
  params.begin_date = {'$lte': today}
  params.end_date = {'$gte': today}
  sorts = {sorder:1}
  dataPage Ad,params,sorts,req.param("pageSize") or req.param("limit"),req.param("pageNum") or req.param("page"), (err,results) ->
    if err
      resErr(res,err)
    else
      if results && results.data
        for dt in results.data
          dt.href = dt.href2 || dt.href
      if req.param("callback")
        res.jsonp results
      else
        res.json(results)

exports.ad_del = (req,res) ->
  if !req.param('_id')
    resErr(res,'编码不得为空')
  else
    _id = req.param('_id')
    Ad.findByIdAndRemove _id,(err)->
      if err
        resErr res,err
      else
        res.json({'success':true})

exports.ad_detail = (req,res) ->
  _id = req.param('_id')
  if !_id
    res.json({'success':'false','err_msg':'编码不得为空'})
  else
    Ad.findById _id, (err,result) ->
      if err
        resErr(res,err)
      else
        res.json(result)

exports.ad_edit = (req,res) ->
  _id = req.param('_id')
  if !_id
    res.json({'success':false,'err_msg':'编码不得为空'})
  params = {}
  params.name = req.param('name')
  params.code = req.param('code')
  params.href = req.param('href')
  params.ad_category = req.param('ad_category')
  params.target = req.param("target")
  params.image_file_id = req.param('image_file_id')
  params.sorder = req.param('sorder')
  params.begin_date = req.param('begin_date')
  params.end_date = req.param('end_date')
  params.updated_at = new Date
  params.content = req.param('content')
  params.status = req.param('status') || '0'
  params.src_site = req.param('src_site')
  params.src_page = req.param('src_page')
  params.src_pos = req.param('src_pos')
  params.src_land = req.param('src_land')
  params.href2 = req.param('href2')
  # if params.href && params.href.indexOf('?') > 0
  #   params.href2 = params.href + '&_gfsrc=' + req.param('src_site') + '_' + req.param('src_page') + '_'+ req.param('src_pos') + '_' + req.param('src_land')'——siparsr！'
  # else
  #   params.href2 = params.href + '?_gfsrc=' + req.param('src_site') + '_' + req.param('src_page') + '_'+ req.param('src_pos') + '_' + req.param('src_land')

  Ad.findByIdAndUpdate _id,params,(err) ->
    if err
      resErr(res,err)
    else
      res.json({'success':'true'})